<template>
  <div v-resize="changeSize" class="diary-box">
    <div class="diary-header">
      <div
        :class="{
          'diary-item': curDirayType !== dirayCollect,
          'diary-item-select': curDirayType === dirayCollect,
        }"
        @click="handleSwitchDiary(dirayCollect)"
      >
        收藏
      </div>
      <div
        :class="{
          'diary-item': curDirayType !== dirayShare,
          'diary-item-select': curDirayType === dirayShare,
        }"
        @click="handleSwitchDiary(dirayShare)"
      >
        共享
      </div>
    </div>
    <div class="diary-info">
      <div v-if="curDirayType === dirayCollect">
        <diray-collect />
      </div>
      <div v-if="curDirayType === dirayShare">
        <diray-share />
      </div>
    </div>
  </div>
</template>

<script>
  import { defineComponent, reactive, toRefs, onMounted, onBeforeUnmount } from 'vue';
  import DirayCollect from '@/views/board-home/components/diray-collect.vue';
  import DirayShare from '@/views/board-home/components/diray-share.vue';
  import {
    HOME_DIRAY_COLLECT,
    HOME_DIRAY_SHARE,
    HOME_DIRAY_COLLECT_HEIGHT,
    HOME_DIRAY_SHARE_HEIGHT,
  } from '@/helper/constant';
  import VueEvent from '@/helper/event';
  import directives from '@/helper/directives';
  export default defineComponent({
    name: 'DirayMain',
    components: { DirayCollect, DirayShare },
    directives: directives,
    setup() {
      const state = reactive({
        curDirayType: HOME_DIRAY_COLLECT, //当前日记类型
        dirayCollect: HOME_DIRAY_COLLECT, //收藏
        dirayShare: HOME_DIRAY_SHARE, //共享
      });
      const methods = {
        handleSwitchDiary(dirayType) {
          state.curDirayType = dirayType;
        },
        changeSize(data) {
          //监听dom宽度高度的变化
          let { width, height } = data;
          let temCollectHeight = parseInt(height) - 125 + 'px';
          let temShareHeight = parseInt(height) - 167 + 'px';
          window.sessionStorage.setItem(HOME_DIRAY_COLLECT_HEIGHT, temCollectHeight);
          window.sessionStorage.setItem(HOME_DIRAY_SHARE_HEIGHT, temShareHeight);
          VueEvent.emit(HOME_DIRAY_COLLECT_HEIGHT);
          VueEvent.emit(HOME_DIRAY_SHARE_HEIGHT);
        },
      };
      onMounted(() => {});
      onBeforeUnmount(() => {
        state.curDirayType = HOME_DIRAY_COLLECT;
      });
      return {
        ...toRefs(state),
        ...methods,
      };
    },
  });
</script>
<style scoped lang="less">
  .diary-box {
    width: 100%;
    flex-grow: 1;
    margin-top: 20px;
    background-color: #ffffff;
    border-radius: 4px;
    .diary-header {
      width: 270px;
      height: 60px;
      margin: 0 auto;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #ecedf0;
      .diary-item {
        margin-right: 30px;
        font-size: 14px;
        color: #3f3f3f;
        cursor: pointer;
      }
      .diary-item:hover {
        color: @primary-color;
      }
      .diary-item-select {
        margin-right: 30px;
        font-size: 18px;
        font-weight: bold;
        border-bottom: 2px solid @primary-color;
      }
    }
    .diary-info {
      width: 100%;
    }
  }
</style>
